/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Exceptions;

using DotNetNuke.Modules.Articles.Data;

#endregion

namespace DotNetNuke.Modules.Articles.Framework
{
    /// -----------------------------------------------------------------------------
    /// Namespace:  DotNetNuke.Modules.Articles.Framework
    /// Project:    Articles Private Assembly
    /// Class:      SectionController
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The SectionController Class represents the Sections Business Layer
    /// Methods in this class call methods in the Data Layer
    /// </summary>
    /// <history>
    /// 	[cnurse]	01/31/2006	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    class SectionController
    {

        #region Public Static Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a Section to the DataStore
        /// </summary>
        /// <param name="section">The section to persist</param>
        /// <param name="userId">The id of the user</param>
        /// <returns>An integer that represents the Id of the new section</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        ///     [cnurse]    04/01/2006  Added SectionSkin and DefaultImageDirectory properties
        /// </history>
        /// -----------------------------------------------------------------------------
        public static int AddSection(SectionInfo section, int userId)
        {
            return DataService.CreateSection(section.ModuleId, section.Name, section.ParentSectionId, section.SectionSkin, section.DefaultImageDirectory, section.ViewOrder, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Deletes a Section from the DataStore
        /// </summary>
        /// <param name="section">The section to delete</param>
        /// <param name="userId">The Id of the user deleting) the section.</param>
        /// <history>
        /// 	[cnurse]	07/19/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void DeleteSection(SectionInfo section, int userId)
        {
            if (section != null)
                DataService.DeleteSection(section.SectionId, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a single section from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of the section</param>
        /// <returns>A SectionInfo object</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static SectionInfo GetSection(int sectionId)
        {
            return CBO.FillObject<SectionInfo>(DataService.GetSection(sectionId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the sections for a module from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the module</param>
        /// <returns>A List of SectionInfo objects</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<SectionInfo> GetSections(int moduleId)
        {
            return CBO.FillCollection<SectionInfo>(DataService.GetSections(moduleId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Child Sections from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of parent Section</param>
        /// <history>
        /// 	[cnurse]	04/03/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<SectionInfo> GetSectionsByParentSection(int moduleId, int sectionId)
        {
            return CBO.FillCollection<SectionInfo>(DataService.GetSectionsByParentSection(moduleId, sectionId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates an existing section (or creates a new one)
        /// </summary>
        /// <param name="section">The section to update</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        ///     [cnurse]    04/01/2006  Added SectionSkin and DefaultImageDirectory properties
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void UpdateSection(SectionInfo section, int userId)
        {
            if (section.SectionId == Null.NullInteger)
                DataService.CreateSection(section.ModuleId, section.Name, section.ParentSectionId, section.SectionSkin, section.DefaultImageDirectory, section.ViewOrder, userId);
            else
                DataService.UpdateSection(section.SectionId, section.Name, section.ParentSectionId, section.SectionSkin, section.DefaultImageDirectory, section.ViewOrder, userId);
        }

        #endregion

    }
}
